//
//  NNHomeViewController.swift
//  DYLive
//
//  Created by 众力金融 on 2019/5/5.
//  Copyright © 2019 众力金融. All rights reserved.
//

import UIKit

class NNHomeViewController: NNBaseViewController, UIScrollViewDelegate {
  
    fileprivate let width = UIScreen.main.bounds.size.width
    fileprivate let height = UIScreen.main.bounds.size.height
    //红色指示器
    fileprivate var indicatorView = UIView()
    fileprivate var selectedButton = UIButton()
    fileprivate var contentView = UIScrollView()
    //titlesView
     fileprivate var titlesView = UIView()
    
    override func viewDidLoad() {
        super.viewDidLoad()
   
        let test: CGFloat = 1.87655
        print(Int(test))
        
//       // 初始化子控制器
       setupChildVces()
//        // 设置顶部的标签栏
//        setupTitlesView()
//        // 底部的scrollView
//        setupContentView()
        
    }
    
    func setupChildVces() {
        
        let vc = NNTestViewController()
        vc.type = 1
        vc.title = "首页"
       
        
        let vc1 = NNTestViewController()
        vc1.type = 2
        vc1.title = "直播"
        
        
        let vc2 = NNTestViewController()
        vc2.type = 3
        vc2.title = "关注"
        
        
        let vc3 = NNTestViewController()
        vc3.title = "新闻"
        
        let vc4 = NNTestViewController()
        vc4.title = "体育明星"
        
        let vc5 = NNTestViewController()
        vc5.title = "头条"
        
        let vc6 = NNTestViewController()
        vc6.title = "突破"
        
        let vc7 = NNTestViewController()
        vc7.title = "勇士我"
        
        let vc8 = NNTestViewController()
        vc8.title = "火箭"
        
        
        let toolView = NNSegmentTool.init(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.size.width, height: 400))
        toolView.subViewControllers.append(vc)
        toolView.subViewControllers.append(vc1)
        toolView.subViewControllers.append(vc2)
        toolView.subViewControllers.append(vc3)
        toolView.titleLabelWidth = 50
        toolView.selectedPosition = 1
        view.addSubview(toolView)
        toolView.refer()
    }

    func setupTitlesView() {
        // 标签栏整体
        titlesView.backgroundColor = .white
        titlesView.frame = CGRect(x: 0, y: 100, width: width, height: 35)
        view.addSubview(titlesView)
        
        indicatorView.backgroundColor = .red
        indicatorView.tag = -1
        indicatorView.height = 2
        indicatorView.y = titlesView.height - indicatorView.height
      
        let width1: CGFloat = titlesView.width / CGFloat(children.count)
        let height1: CGFloat = titlesView.height
        
        for i in 0..<children.count {
            let button = UIButton()

            button.tag = i + 100
            button.height = height1
            button.width = width1
            button.x = CGFloat(CGFloat(i) * width1)
            let vc = children[i]
            button.setTitle(vc.title, for: .normal)
            button.setTitleColor(.gray, for: .normal)
            button.setTitleColor(.red, for: .disabled)
            button.titleLabel?.font = UIFont.systemFont(ofSize: 14)
            button.addTarget(self, action: #selector(buttonCLick(_:)), for: .touchUpInside)
            titlesView.addSubview(button)
            
            if i == 0 {
              button.isEnabled = false
              selectedButton = button
              
                button.titleLabel?.sizeToFit()
                indicatorView.width = button.titleLabel!.width
                indicatorView.centerX = button.centerX
                
            }
            
        }
        
        titlesView.addSubview(indicatorView)
    }
    
    @objc func buttonCLick(_ button:UIButton) {

        selectedButton.isEnabled = true
        button.isEnabled = false
        selectedButton = button
        
        UIView.animate(withDuration: 0.25) { [unowned self] in
            self.indicatorView.width = button.titleLabel!.width
            self.indicatorView.centerX = button.centerX
        }
        
        var offset: CGPoint = contentView.contentOffset
        offset.x = CGFloat(button.tag - 100) * contentView.width
        contentView.setContentOffset(offset, animated: true)
    }
    // MARK: - 底部的scrollView
    func setupContentView() {
        contentView.frame = view.bounds
        contentView.delegate = self
        contentView.isPagingEnabled = true
        contentView.showsVerticalScrollIndicator = false
        contentView.showsHorizontalScrollIndicator = false
        view.insertSubview(contentView, at: 0)
        contentView.contentSize = CGSize(width: Int(contentView.width) * children.count, height: 0)
        scrollViewDidEndScrollingAnimation(contentView)
    }
    // MARK: - <UIScrollViewDelegate>
     func scrollViewDidEndScrollingAnimation(_ scrollView: UIScrollView) {
        
        let index: Int = Int(scrollView.contentOffset.x / scrollView.width)
        let vc = children[index]
        vc.view.x = scrollView.contentOffset.x
        vc.view.y = 135
        vc.view.height = scrollView.height
        scrollView.addSubview(vc.view)
    }
    
    func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
        scrollViewDidEndScrollingAnimation(scrollView)

    }
    
    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        let index: Int = Int(scrollView.contentOffset.x / scrollView.width)
        let button = titlesView.viewWithTag(index + 100) as! UIButton
        selectedButton.isEnabled = true;
        button.isEnabled = false;
        selectedButton = button;
      //  buttonCLick( as! UIButton)
       
    }
   
}
